package com.minggo.charmword.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.minggo.charmword.R;
import com.minggo.charmword.bitmap.BitmapDisplayConfig;
import com.minggo.charmword.bitmap.FinalBitmap;
import com.minggo.charmword.model.Around;

/**
 * 适配器
 * @author minggo
 * @date 2014-10-9 下午5:39:01
 */
public class ShakeFindAdapter extends BaseAdapter {
	
	private Context context;
	private List<Around> list;
	private FinalBitmap fb;
	private BitmapDisplayConfig bitmapDisplayConfig;
	
	public ShakeFindAdapter(Context context,List<Around> list){
		this.context = context;
		this.list = list;
		
		fb = FinalBitmap.create(context);

		bitmapDisplayConfig = new BitmapDisplayConfig();

		bitmapDisplayConfig.setAnimation(null);
		bitmapDisplayConfig.setAnimationType(BitmapDisplayConfig.AnimationType.fadeIn);
		bitmapDisplayConfig.setLoadingBitmap(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.img_avatar_default));
		bitmapDisplayConfig.setLoadfailBitmap(BitmapFactory.decodeResource(context.getResources(),
				R.drawable.img_avatar_default));

	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView==null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_shake_around, null);
			viewHolder.addressTv = (TextView) convertView.findViewById(R.id.tv_address);
			viewHolder.userNameTv = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.wordTv = (TextView) convertView.findViewById(R.id.tv_word);
			viewHolder.explainTv = (TextView) convertView.findViewById(R.id.tv_explain);
			viewHolder.avatarIv = (ImageView) convertView.findViewById(R.id.iv_user_avatar);
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.addressTv.setText(list.get(position).simpleAddess);
		viewHolder.userNameTv.setText(list.get(position).userName);
		viewHolder.wordTv.setText(list.get(position).word);
		viewHolder.explainTv.setText(list.get(position).explain);
		
		fb.displayRound(viewHolder.avatarIv,list.get(position).userAvatar,bitmapDisplayConfig,true);
		return convertView;
	}
	
	private class ViewHolder{
		ImageView avatarIv;
		TextView userNameTv;
		TextView wordTv;
		TextView explainTv;
		TextView addressTv;
	}
	public void recycleFinalBitmap(){
		fb.clearMemeoryCache();
		//fb = null;
		//bitmapDisplayConfig = null;
	}
}
